#!/usr/bin/env python
# -*- coding: utf-8 -*-
# --------------------------------------------------------
# Descripttion:
# version: 0.0.1
# Author: Wenwei Fu (2516800961@qq.com)
# FilePath: /fastapi_byd/app/api/routes/expert.py
# Create: 2021-01-04 16:37:40
# LastAuthor: Wenwei Fu
# lastTime: 2021-01-10 15:01:42
# --------------------------------------------------------

import uuid
import datetime
from typing import Optional
from pydantic import BaseModel
from fastapi import APIRouter, Request, Body, Depends, HTTPException
from app.api.dependencies.authentication import get_current_user_authorizer
from app.db.schemas import Traffic_Road # 道路
from app.db.database import get_db

router = APIRouter()

@router.get("/list", name="查询道路列表", dependencies=[Depends(get_db)])
async def road_list(
    pageNum: Optional[int] = 1,
    pageSize: Optional[int] = 10,
    request: Request=None
):
    query = Traffic_Road.select().order_by(-Traffic_Road.created_at).paginate(pageNum, pageSize).dicts()
    results = [item for item in query]
    count = Traffic_Road.select().count()
    return {
        "code": 200,
        "message": "查询成功",
        "data": {
            "pageNum": pageNum,
            "pageSize": pageSize,
            "total": count,
            "records": results
        }
    }

@router.get("/dicts", name="查询道路 字典", dependencies=[Depends(get_db)])
async def road_dicts(
    request: Request=None
):
    query = Traffic_Road.select().order_by(-Traffic_Road.created_at).dicts()
    results = [item for item in query]
    return {
        "code": 200,
        "message": "查询成功",
        "data": results
    }

@router.post("/create", name="创建道路", dependencies=[Depends(get_db), Depends(get_current_user_authorizer())])
async def create_road(
    road_name: str,
    road_lonlat: Optional[str] = None,
    created_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    updated_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    request: Request = None
):
    results = {}
    results["road_id"] = str(uuid.uuid1())[:6]
    if road_name is not None:
        results["road_name"] = road_name
    if road_lonlat is not None:
        results["road_lonlat"] = road_lonlat
    Traffic_Road.create(**results)
    print('创建道路', results)

    return {
        "code": 200,
        "message": "创建成功",
        "data": results
    }

@router.put("/update", name="更新道路", dependencies=[Depends(get_db), Depends(get_current_user_authorizer())])
async def update_road(
    id: str,
    road_name: str,
    road_lonlat: Optional[str] = '',
    request: Request = None
):
    updatesql = (Traffic_Road.update(
                    road_name=road_name,
                    road_lonlat=road_lonlat,
                    updated_at=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                ).where(Traffic_Road.id == id)
                .execute())
    if updatesql == 1:
        return {
                "code": 200,
                "message": "更新成功",
                "data": {
                    "id": id,
                    "road_name": road_name
                }
            }
    else:
        return {
                "code": 201,
                "message": "更新失败",
                "data": {
                    "id": id,
                    "road_name": road_name
                }
            }

@router.delete("/delete", name="删除道路", dependencies=[Depends(get_db), Depends(get_current_user_authorizer())])
async def delete_road(
    id: str,
    request: Request=None
):
    query = Traffic_Road.delete().where(Traffic_Road.id == id).execute()
    if query == 1:
        return {
            "code": 200,
            "message": "删除成功",
            "data": {
                "id": id
            }
        }
    else:
        return {
            "code": 200,
            "message": "删除失败",
            "data": {
                "id": id
            }
        }